#include <iostream>
#include <stdio.h>

#include <iostream>
#include <memory>


class IntSLLNode{
public:
    IntSLLNode(){
        next = 0;
    }

    IntSLLNode(int el, IntSLLNode *ptr = 0){
        info = el; next = ptr;
    }
    int info;
    IntSLLNode *next;
};

class IntSLList{
    public:
    IntSLList(){
        head = tail = 0;
    }

    ~IntSLList();

    int isEmpty(){
        return head == 0;
    }

    void addToHead(int);
    void addToTail(int);
    int deleteFromHead();
    int deleteFromTail();
    void deleteNode(int);
    bool isInlist(int) const;

private:
    IntSLLNode *head, *tail;
};

IntSLList::~IntSLList()
{
    for(IntSLLNode *p; !isEmpty();){
        p = head->next;
        delete head;
        head = p;
    }
}

void IntSLList::addToHead(int el)
{
    head = new IntSLLNode(el,head);
    if(tail ==0)
        tail = head;
}

void IntSLList::addToTail(int el)
{
    if(tail !=0){
        tail->next = new IntSLLNode(el);
        tail = tail->next;
    }else{
        head = tail = new IntSLLNode(el);
    }
}

int IntSLList::deleteFromHead()
{
    int el = head->info;
    IntSLLNode *tmp = head;
    if(head == tail)
        head = tail = 0;
    else 
        head = head->next;
    delete tmp;
    return el;
}

int IntSLList::deleteFromTail()
{
    int el = tail->info;
    if(head == tail){
        delete head;
        head = tail = 0;
    }else{
        IntSLLNode *tmp;
        for(tmp = head; tmp->next != tail; tmp = tmp->next)

        delete tail;
        tail = tmp;
        tail->next = 0;
    }

    return 0;
}

void IntSLList::deleteNode(int el)
{
    if(head == 0)
        return;
    if(head == tail && el == head->info){
        delete head;
        head = tail = 0;
    }else if(el == head->info){
        IntSLLNode *tmp = head;
        head = head->next;
        delete tmp;
    }else {
        IntSLLNode *pred, *tmp;
        for(pred = head, tmp = head->next; tmp != 0 && !(tmp->info == el); pred = pred->next, tmp = tmp->next);

        if(tmp != 0){
            pred->next = tmp->next;
            if(tmp == tail)
                tail = pred;
            delete tmp;
        }

    }
}

bool IntSLList::isInlist(int el)const
{
    IntSLLNode *tmp;
    for(tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next)

    return tmp != 0;
}

int main(int argv, char**argc)
{
    printf("************  test hello zc!!!   **************\n");
    /* add test code! */
    
    return 0;
}
